﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using NewsAggregatorLibrary;

namespace NewsAggregator.WebService
{
    /// <summary>
    /// Summary description for DataService
    /// </summary>
     // [ScriptService]
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class DataService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(EnableSession = true)]
        public StoriesMemberCollection GetStoriesByKeywordSearch(string keyword, int pageNumber)
        {
            try
            {
                StoriesMemberCollection stories = StoriesMember.RetrieveByKeywordSearch(keyword);

                StoriesMemberCollection returnStories = new StoriesMemberCollection();
                int numberOfObjectsPerPage = 10;
                returnStories.AddRange(stories
                  .Skip(numberOfObjectsPerPage * pageNumber)
                  .Take(numberOfObjectsPerPage));
                return returnStories;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        [WebMethod(EnableSession = true)]
        public StoriesMemberCollection GetRelatedStories(string storyID, int pageNumber)
        {
            try
            {
                StoriesMemberCollection stories = StoriesMember.RetrieveRelated(int.Parse(storyID));

                StoriesMemberCollection returnStories = new StoriesMemberCollection();
                int numberOfObjectsPerPage = 10;
                returnStories.AddRange(stories
                  .Skip(numberOfObjectsPerPage * pageNumber)
                  .Take(numberOfObjectsPerPage));
                return returnStories;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        [WebMethod(EnableSession = true)]
        public StoriesMemberCollection GetStoriesByCategory(string categoryid, int pageNumber)
        {
            try
            {
                StoriesMemberCollection stories = StoriesMember.RetrieveByCategoryID(int.Parse(categoryid));

                StoriesMemberCollection returnStories = new StoriesMemberCollection();
                int numberOfObjectsPerPage = 10;
                returnStories.AddRange(stories
                  .Skip(numberOfObjectsPerPage * pageNumber)
                  .Take(numberOfObjectsPerPage));
                return returnStories;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
